package com.leetcode.algorithm.y22.m08.w2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1282. 用户分组
 * 
 * https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/
 * 
 * @author jie.deng
 *
 */
class Question1282Solution01 {

	public List<List<Integer>> groupThePeople(int[] groupSizes) {
		List<List<Integer>> result = new ArrayList<>();
		Map<Integer, List<Integer>> map = new HashMap<>();
		for (int i = 0; i < groupSizes.length; i++) {
			int groupSize = groupSizes[i];
			List<Integer> list = map.get(groupSize);
			if (list == null) {
				list = new ArrayList<>(groupSize);
				map.put(groupSize, list);
			}
			list.add(i);
			if (list.size() == groupSize) {
				result.add(new ArrayList<>(list));
				list.clear();
			}
		}

		return result;
	}

}